package cn.muqingfeng.bbs.ui;

import java.util.HashMap;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;
import cn.muqingfeng.api.RequestHelper;
import cn.muqingfeng.api.UriHelper;
import cn.muqingfeng.api.request.GsonRequest;
import cn.muqingfeng.api.to.BaseResponseTO;
import cn.muqingfeng.bbs.R;
import cn.muqingfeng.util.AccountManager;

import com.android.volley.Request.Method;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;

public class ChangePassActivity extends BaseActivity implements OnClickListener {
	private static final Object LOCK = new Object();
	private EditText mOldPassView;
	private EditText mNewPassView;
	private EditText mRePassView;
	private boolean isChange = false;
	private String mOldPassword;
	private ProgressDialog mDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.change_pwd);
		setupViews();
	}

	private void setupViews() {
		findViewById(R.id.back).setOnClickListener(this);
		mOldPassView = (EditText) findViewById(R.id.old_password);
		mNewPassView = (EditText) findViewById(R.id.password);
		mRePassView = (EditText) findViewById(R.id.repassword);
		findViewById(R.id.submit).setOnClickListener(this);
		mOldPassword = AccountManager.getPassword(getApplicationContext());
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		case R.id.submit:
			changePassword();
			break;
		default:
			break;
		}
	}

	private void changePassword() {
		synchronized (LOCK) {
			if (isChange)
				return;
			String oldPass = mOldPassView.getText().toString();
			if (TextUtils.isEmpty(oldPass)) {
				Toast.makeText(getApplicationContext(), "请输入原密码", Toast.LENGTH_SHORT).show();
				return;
			}
			if (!oldPass.equals(mOldPassword)) {
				Toast.makeText(getApplicationContext(), "原密码不正确", Toast.LENGTH_SHORT).show();
				return;
			}
			final String newPass = mNewPassView.getText().toString();
			if (TextUtils.isEmpty(newPass) || newPass.length() < 6) {
				Toast.makeText(getApplicationContext(), "请填写新密码, 最小长度为 6 个字符", Toast.LENGTH_SHORT).show();
				return;
			}
			String rePass = mRePassView.getText().toString();
			if (!newPass.equals(rePass)) {
				Toast.makeText(getApplicationContext(), "两次输入密码不一致", Toast.LENGTH_SHORT).show();
				return;
			}
			isChange = true;
			showDialog();
			String url = UriHelper.getChangePasswordUrl();
			HashMap<String, String> params = new HashMap<String, String>();
			params.put(UriHelper.PARAMS_OLD_PASSWORD, oldPass);
			params.put(UriHelper.PARAMS_NEW_PASSWORD, newPass);
			params.put(UriHelper.PARAMS_NEW_PASSWORD2, rePass);
			GsonRequest<BaseResponseTO> request = new GsonRequest<BaseResponseTO>(Method.POST, url, new Listener<BaseResponseTO>() {

				@Override
				public void onResponse(BaseResponseTO response) {
					isChange = false;
					dismissDialog();
					if (!isDestroied()) {
						if (response == null) {
							Toast.makeText(getApplicationContext(), "修改失败，请重试", Toast.LENGTH_SHORT).show();
							return;
						} else {
							if (response.isSuccess()) {
								Toast.makeText(getApplicationContext(), "修改成功", Toast.LENGTH_SHORT).show();
								AccountManager.setPassword(getApplicationContext(), newPass);
								finish();
							} else {
								String errormsg = response.getMessage();
								errormsg = TextUtils.isEmpty(errormsg) ? "修改失败，请重试" : errormsg;
								Toast.makeText(getApplicationContext(), errormsg, Toast.LENGTH_SHORT).show();
							}
						}
					}
				}
			}, new ErrorListener() {

				@Override
				public void onErrorResponse(VolleyError error) {
					isChange = false;
					dismissDialog();
					Toast.makeText(getApplicationContext(), "修改失败，请重试", Toast.LENGTH_SHORT).show();
				}
			}, params, BaseResponseTO.class);
			request.setNeedLogin(true);
			RequestHelper.startRequest(getApplicationContext(), request);
		}
	}

	private void showDialog() {
		if (mDialog != null && mDialog.isShowing()) {
			return;
		}
		mDialog = new ProgressDialog(this);
		mDialog.setMessage("正在请求，请稍等...");
		mDialog.show();
	}

	private void dismissDialog() {
		if (mDialog != null && mDialog.isShowing()) {
			mDialog.dismiss();
		}
	}
}
